﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *
 **/

using System;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Input;
using Peter.Common;
using Peter.Common.Interfaces;
using Peter.Common.MainMenu;
using Peter.Common.Utilities;
using Peter.Sdw.Logic;
using Peter.Sdw.Logic.Dialogs;
using Peter.Sdw.View;
using Peter.Sdw.View.Dialogs;

namespace Peter.Sdw
{
   /// <summary>
   /// Main plugin for seek deep within.
   /// </summary>
   public class SdwPlugin
   {
      private SdwMainModel m_SdwMain;
      private RelayCommand m_CmdShowSdw;
      private IPeter m_Peter;

      /// <summary>
      /// Initializes a new sdw plugin.
      /// </summary>
      public SdwPlugin ()
      {
         var dictionary = new ResourceDictionary {Source = new Uri ("pack://application:,,,/Peter.Sdw;Component/Resources/SdwStyles.xaml")};
         Application.Current.Resources.MergedDictionaries.Add (dictionary);
         ViewRegistry.Register (typeof (LoginModel), typeof (LoginView));
         ViewRegistry.Register (typeof (BookModel), typeof (BookView));
         ViewRegistry.Register (typeof (SdwMainModel), typeof (SdwMainView));
         ViewRegistry.Register (typeof (TermAddModel), typeof (TermAddView));
         ViewRegistry.Register (typeof (SelectTermModel), typeof (SelectTermView));
      }

      /// <summary>
      /// Gets the main view model.
      /// </summary>
      [Export (typeof (PeterDockItem))]
      [ExportMetadata ("ContentId", SdwMainModel.GUID)]
      public SdwMainModel SdwMain
      {
         get { return this.m_SdwMain ?? (this.m_SdwMain = new SdwMainModel ()); }
      }

      /// <summary>
      /// Gets or Sets the instance of peter.
      /// </summary>
      [Import (typeof (IPeter))]
      public IPeter Peter
      {
         get { return this.m_Peter; }
         set
         {
            this.m_Peter = value;
            this.m_Peter.ShuttingDown += this.OnShutDown;
         }
      }

      /// <summary>
      /// Gets the command that shows the main window.
      /// </summary>
      public ICommand ShowSdwCommand
      {
         get { return this.m_CmdShowSdw ?? (this.m_CmdShowSdw = new RelayCommand (this.OnShow)); }
      }

      /// <summary>
      /// Occurs when the sdw show button is clicked.
      /// </summary>
      /// <param name="obj">Command parameter.</param>
      private void OnShow (object obj)
      {
         this.Peter.AddToolWindow (this.SdwMain);
      }

      /// <summary>
      /// Gets the show sdw window menu item.
      /// </summary>
      [Export (typeof (IMainMenuItem))]
      public IMainMenuItem SdwShowMenuItem
      {
         get
         {
            return new PeterMainMenuItem
            {
               Priority = 10,
               HeaderName = "view",
               Command = this.ShowSdwCommand,
               ToolTip = "Show SDW Main Window",
               Icon = MainMenuIcon.Book
            };
         }
      }

      /// <summary>
      /// Occurs when peter is shutting down.
      /// </summary>
      /// <param name="sender">Peter</param>
      /// <param name="e">Event args.</param>
      private void OnShutDown (object sender, EventArgs e)
      {
         this.m_Peter.ShuttingDown -= this.OnShutDown;
         WebQueue.Instance.Dispose ();
      }
   }
}
